<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-12-01 16:20
 *
 * Version: 2.0
 */

namespace App\Modules\Gift\Repositories;


use App\Models\GiftSend;
use Flower\Core\Repository\Eloquent\BaseRepository;

class GiftSendRepository extends BaseRepository
{
	public function model()
	{
		return GiftSend::class;
	}

	/**
	 * 发礼物记录
	 * @param $giftId
	 * @param $memberId
	 * @param $userId
	 * @param $money
	 * @return mixed
	 */
	public function add($giftId,$memberId,$userId,$money,$liveId){
		$data = [
			'gift_id' => $giftId,
			'member_id' => $memberId,
			'user_id' => $userId,
			'send_start_time' => date('Y-m-d H:i:s'),
			'created_at' => date('Y-m-d H:i:s'),
			'updated_at'=> date('Y-m-d H:i:s'),
			'money' => $money,
			'live_id' =>$liveId
		];
		return $this->model->insert($data);
	}



	public function lst($userId,$liveId=0){
		$model = $this->model->select(['id','gift_id','member_id','send_start_time','money'])->with('gift:id,name,image','member:id,name,face')->with(['gift'=>function($v){
			$v->select(['gift.id', 'gift.name', 'picture.image'])->leftJoin('picture', 'picture.id', 'gift.image');
		}])->where(['user_id'=>$userId])->whereDate('created_at','=',date('Y-m-d'));
		return empty($liveId) ? $model->orderBy('id','desc')->get() :  $model->where(['live_id'=>$liveId])->orderBy('id','desc')->get();
	}
}